package com.example.imiaow.yyads_text.splash;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.example.imiaow.yyads_text.Config;
import com.youyi.yesdk.ad.SplashAd;
import com.youyi.yesdk.business.AdPlacement;
import com.youyi.yesdk.listener.SplashListener;

import org.jetbrains.annotations.Nullable;

import java.util.Map;

import io.flutter.Log;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.platform.PlatformView;

public class Splash implements PlatformView {
    private final Context mcontext;
    private final Activity mactivity;
    private final BinaryMessenger mbinaryMessenger;
    private final int mid;


    private SplashAd splashAd;
    private FrameLayout mframeLayout;
    private MethodChannel channel;


    public Splash(
            Context context,
            Activity activity,
            BinaryMessenger binaryMessenger,
            int id

    ) {
        this.mcontext = context;
        this.mactivity = activity;
        this.mbinaryMessenger = binaryMessenger;
        this.mid = id;
    }

    public void init(){
        if(mactivity == null){
            TextView textView = new TextView(mcontext);
            textView.setText("");
            mframeLayout = new FrameLayout(mcontext);
            mframeLayout.addView(textView);
        }else{
            Config config = new Config();
            channel = new MethodChannel(mbinaryMessenger,config.splishPage+"_"+mid);
            splashAd = new SplashAd();
            mframeLayout = new FrameLayout(mcontext);
            splashAd.setSplashConfig(
                    this.mactivity,
                    new AdPlacement.Builder()
                            .setAdId("0000000467")
                            .build()
            );
            splashAd.loadSplashAd(mframeLayout, new SplashListener() {
                @Override
                public void onError(@Nullable Integer integer, @Nullable String s) {
                    channel.invokeMethod("onError",s);
                }

                @Override
                public void onTimeOut() {
                    channel.invokeMethod("onTimeOut","android_加载超时");
                }

                @Override
                public void onAdShow() {
                    channel.invokeMethod("onAdShow","android_开屏广告展示成功");
                }

                @Override
                public void onAdCanceled() {
                    channel.invokeMethod("onAdCanceled","android_跳过广告");
                }

                @Override
                public void onAdClicked() {
                    channel.invokeMethod("onAdClicked","android_点击开屏广告");
                }
            });
        }

    }

    @Override
    public View getView() {
        return mframeLayout;
    }

    @Override
    public void dispose() {
        if(splashAd != null){
            splashAd.destroy();
        }
    }
}
